<template>
  <div class="chat-box" >
    <h3 style="margin: 20px">
      某某某
    </h3>
    <!-- 聊天记录列表 -->
    <div class="chat-messages" ref="messages">
      <div v-for="(message, index) in messages" :key="index" :class="{ me: message.isMe }">
        {{ message.text }}
      </div>
    </div>

    <!-- 输入框和发送按钮 -->
    <div style="position: absolute;width: 800px">
      <div class="chat-input">
        <input type="text" v-model="newMessage" @keyup.enter="sendMessage" placeholder="请输入" style="height: 30px">
        <button @click="sendMessage" style="height: 30px">Send</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name:"Conversation",
  data() {
    return {
      messages: [
        { text: '您好!我暂时不在，请留言', isMe: false },
        { text: '', isMe: true }
      ],
      newMessage: ''
    };
  },
  methods: {
    sendMessage() {
      if (this.newMessage.trim()) {
        this.messages.push({ text: this.newMessage, isMe: true });
        this.newMessage = ''; // 清空输入框
        this.scrollToBottom(); // 滚动到聊天框底部
      }
    },
    scrollToBottom() {
      const messagesElement = this.$refs.messages;
      messagesElement.scrollTop = messagesElement.scrollHeight;
    }
  }
};
</script>

<style scoped>
.chat-box {
  width:800px;
  margin: 50px auto;
  border: 1px solid #ccc;
  height: 600px;
}

.chat-messages {
  max-height: 300px;
  overflow-y: auto;
  padding: 10px;
  margin-bottom: 50px;
  height: 750px;
}

.me {
  text-align: right;
  margin-bottom: 10px;
}

.chat-input {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
}

.chat-input input {
  flex-grow: 1;
  margin-right: 10px;
}
</style>
